package cn.fanyamin.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ToolService;
import cn.fanyamin.domain.Article;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.User;

public class ArticleAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(ArticleAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		User user = getUserFromSession(request);
		
		
		int categoryID = NumberUtils.toInt(request.getParameter("categoryID"));
        
		setCategories(request, UserCategory.CATEGORY_TYPE_LORE);

	    List<Article> articleList = null;
	    if(categoryID==0) {
	        articleList = toolService.listArticle(user.getUserID());
	    } else {
	        Article aArticle = new Article();
	        aArticle.setUserID(user.getUserID());
	        aArticle.setCategoryID(categoryID);
	        articleList = toolService.findArticle(aArticle);
	    }
	    request.setAttribute("categoryID", categoryID);
	    request.setAttribute("articleList", articleList);
		//logger.info("articleList: " + articleList);
		return new ActionForward("/WEB-INF/jsp/struts/tool/articles.jsp");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		Article article = getArticleFromForm(form);
		if(article == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		article.setUserID(user.getUserID());
		int articleID = toolService.createArticle(article);
		article.setArticleID(articleID);
		logger.info("create article: " + article);
		//return mapping.findForward("success");
		return new ActionForward("article.htm?method=list",true);
	}
	
    private Article getArticleFromForm(ActionForm form) {
		Article article = new Article();
		LazyValidatorForm articleForm = (LazyValidatorForm) form;

		article.setArticleID(NumberUtils.toInt((String) articleForm.get("articleID")));
		article.setTopic((String) articleForm.get("topic"));	
		article.setContent((String) articleForm.get("content"));	
		article.setKeywords((String) articleForm.get("keywords"));
		article.setCategoryID(NumberUtils.toInt((String)articleForm.get("categoryID")));

		return article;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm articleFrom = (LazyValidatorForm)form;
        int articleID = NumberUtils.toInt((String)articleFrom.get("articleID"));
        Article article = toolService.retrieveArticle(articleID);
        request.setAttribute("article", article);
        return new ActionForward("/WEB-INF/jsp/struts/tool/articleInfo.jsp");
    }
    
        
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ToolService toolService = getWebApp().getToolService();
        Article article = getArticleFromForm(form);
        toolService.updateArticle(article);
        //request.setAttribute("articleType", article.getArticleType());
        logger.info("update article, article=" + article);
        return new ActionForward("article.htm?method=list",true);
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm articleFrom = (LazyValidatorForm)form;
        int articleID = NumberUtils.toInt((String)articleFrom.get("articleID"));
        toolService.deleteArticle(articleID);
        int articleType = NumberUtils.toInt(request.getParameter("articleType"));
        //request.setAttribute("articleType", articleType);
        logger.info("delete article, articleID=" + articleID);
        //return mapping.findForward("success");
        return new ActionForward("article.htm?method=list",true);//redirect
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add article");
    	
    	setCategories(request, UserCategory.CATEGORY_TYPE_LORE);
    	
    	return new ActionForward("/WEB-INF/jsp/struts/tool/article.jsp");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm articleFrom = (LazyValidatorForm)form;
        int articleID = NumberUtils.toInt((String)articleFrom.get("articleID"));
        Article article = toolService.retrieveArticle(articleID);
        request.setAttribute("article", article);
        
        setCategories(request, UserCategory.CATEGORY_TYPE_LORE);
        return new ActionForward("/WEB-INF/jsp/struts/tool/article.jsp");
    }
}
